﻿using IntelligentGateway.Data;
using IntelligentGateway.Model;
using IntelligentGateway.Tools;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace IntelligentGateway.Services
{
    public class SerialMCMessageProcess
    {
        static SignalPointDA signalPointDA = new SignalPointDA();
        public static bool ProcessSerialData(byte[] data)
        {
            if(data.Length!=22) return false;
            if (data[0] != 0x02 && data[21]!=0x03) return false;
            if (data[1] == 0x61 && data[2]==0x64) //开锁联动,电梯召唤
            {
                byte[] bytes = ParseData(data);
                //TODO 发送数据CEMS
                int fromv = BitConverter.ToInt32(new byte[] { bytes[3], bytes[2], bytes[1], bytes[0] });
                int tov = BitConverter.ToInt32(new byte[] { bytes[7], bytes[6], bytes[5], bytes[4] });
                if(fromv != -1)
                {
                    SetOutSideOutCallData(fromv,true);
                    SetInCallData(tov);
                }
                else
                {
                    SetInSideOutCallData(tov,1);
                }
              //  SetInCallData(inv);
            }
            else if(data[1] == 0x61 && data[2] == 0x63)//互访
            {
                byte[] bytes = ParseData(data);
                int fromv = BitConverter.ToInt32(new byte[] { bytes[3], bytes[2], bytes[1], bytes[0] });
                int tov = BitConverter.ToInt32(new byte[] { bytes[7], bytes[6], bytes[5], bytes[4] });
                int iFrom = int.Parse(fromv.ToString().Substring(fromv.ToString().Length-4,2));
                int iTo = int.Parse(tov.ToString().Substring(tov.ToString().Length-4,2));
                if(iFrom>iTo)
                    SetInSideOutCallData(fromv, 1);
                else SetInSideOutCallData(fromv,2);
                Thread.Sleep(1000);
                SetInCallData(tov);
            }
            return true;
        }
        private static byte[] ParseData(byte[] data)
        {
            byte[] buffer = new byte[8];           
            int index = 0;
            for (int i = 3; i <= 10; i += 2)
            {
                char a = Convert.ToChar(Convert.ToByte(data[i].ToString()));
                char b = Convert.ToChar(Convert.ToByte(data[i + 1].ToString()));
                string str = string.Concat(a, b);
                buffer[index] = Convert.ToByte(str, 16);
                index++;
            }           
            for (int i = 11; i <= 18; i += 2)
            {
                char a = Convert.ToChar(Convert.ToByte(data[i].ToString()));
                char b = Convert.ToChar(Convert.ToByte(data[i + 1].ToString()));
                string str = string.Concat(a, b);
                buffer[index] = Convert.ToByte(str, 16);
                index++;
            }
            return buffer;
        }
        private static bool SetOutSideOutCallData(int Data, bool DFloor = false)
        {
            Data = Data - 100000000;
            string strData = Data.ToString().PadLeft(8,'0');
            int buidingID = int.Parse(strData.Substring(0, 2));
            int unitID = int.Parse(strData.Substring(2, 2));
            int no = int.Parse(strData.Substring(4, 4));
            int floorID = ConvertFloorID(no);
            SignalPoint point = signalPointDA.GetSignalPointData(floorID, 1,DFloor);
            if(point != null)
            {
                IGTCPServerMessageProcess.ProcessOutCall(point);
            }
            return true;
        }
        private static bool SetInSideOutCallData(int Data,int TypeID, bool DFloor = false)
        {
            string strData = Data.ToString().PadLeft(8, '0');
            int buidingID = int.Parse(strData.Substring(0, 2));
            int unitID = int.Parse(strData.Substring(2, 2));
            int floorID = int.Parse(strData.Substring(4,2));
            //int floorID = ConvertFloorID(no);
            SignalPoint point = signalPointDA.GetSignalPointData(floorID, TypeID,DFloor);
            if (point != null)
            {
                IGTCPServerMessageProcess.ProcessOutCall(point);
            }
            return true;
        }
        private static bool SetInCallData(int Data)
        {
            string strData = Data.ToString();
            int buidingID = 0;
            int unitID = 0;
            int FloorID =0;
            int InNO=0;
            if (strData.Length == 7)
            {
                buidingID = int.Parse(strData.Substring(0, 1));
                unitID = int.Parse(strData.Substring(1, 2));
                FloorID = int.Parse(strData.Substring(3, 2));
                InNO = int.Parse(strData.Substring(5, 2));
            }
            else
            {
                buidingID = int.Parse(strData.Substring(0, 2));
                unitID = int.Parse(strData.Substring(2, 2));
                FloorID = int.Parse(strData.Substring(4, 2));
                InNO = int.Parse(strData.Substring(6, 2));
            }
            SignalPoint point = signalPointDA.GetSignalPointData(FloorID, 0,false);
            if (point != null)
            {
                IGTCPServerMessageProcess.ProcessInCall(point);
            }
            return true;
        }
        private static int ConvertFloorID(int NO)
        {           
            if (NO >= 10)
            {
                if (NO == 11) return 1;
                else
                {
                    return -(NO - 10) - 1;
                }
            }
            else
            {
                if(NO==1) return 1;
                else
                {
                    return -NO + 1;
                }
            }           
        }
    }
}
